import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 功能:修改用户密码
 * SQL语句:UPDATE userinfo SET password='871016' WHERE username='Tony'
 * 数据库查询确认: SELECT * FROM userinfo;
 */
public class JDBCDemo2 {
    public static void main(String[] args) {

        try {
            // 1.加载数据库驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2.使用数据库连接对象
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/tedu","root","root");
            // 3.创建数据库执行对象
            Statement statement = connection.createStatement();
            // 4.执行SQL语句

            /*
                接收用户输入
                1.请输入用户名
                2.请输入密码
             */
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入用户名:");
            String username=sc.nextLine();
            System.out.println("请输入密码:");
            String password=sc.nextLine();

            String sql= "UPDATE userinfo SET password='"+password+"' WHERE username='"+username+"' ";
            int i = statement.executeUpdate(sql);
            // 5.断开连接
            connection.close();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}
